import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class FenetreChat extends JFrame {
	private JTextField textField;
	private Client controleur;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	
	public FenetreChat(Client controleur) {
		this.controleur = controleur;
		this.setBounds(50, 50, 800,598);
		setTitle("Tchat");
		getContentPane().setLayout(null);
		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				fermerFenetre();
			}
		});
		
		JTextArea textArea = new JTextArea();
		textArea.setBounds(191, 13, 410, 489);
		getContentPane().add(textArea);
		
		JList list = new JList();
		list.setBounds(12, 12, 167, 527);
		getContentPane().add(list);
		
		JList list_1 = new JList();
		list_1.setBounds(613, 12, 167, 527);
		getContentPane().add(list_1);
		
		textField = new JTextField();
		textField.setBounds(190, 514, 307, 25);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("Envoyer");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnNewButton.setBounds(509, 514, 92, 25);
		getContentPane().add(btnNewButton);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnFichier = new JMenu("Fichier");
		menuBar.add(mnFichier);
		
		JMenuItem mntmSeDconnecter = new JMenuItem("Se déconnecter");
		mntmSeDconnecter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				confirmerDeconnexion();
			}
		});
		mnFichier.add(mntmSeDconnecter);
		
		JMenuItem mntmQuitter = new JMenuItem("Quitter");
		mntmQuitter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fermerFenetre();
			}
		});
		mnFichier.add(mntmQuitter);
		
		JMenu mnSalon = new JMenu("Salon");
		menuBar.add(mnSalon);
		
		JMenuItem mntmCrerUnSalon = new JMenuItem("Créer un salon");
		mntmCrerUnSalon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ouvrirDialogCreerSalon();
			}
		});
		mnSalon.add(mntmCrerUnSalon);
		
		JMenuItem mntmRejoindreUnSalon = new JMenuItem("Rejoindre un salon");
		mntmRejoindreUnSalon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					ouvrirDialogRejoindreSalon();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		mnSalon.add(mntmRejoindreUnSalon);
		
		JMenuItem mntmSupprimerUnSalon = new JMenuItem("Supprimer un salon");
		mntmSupprimerUnSalon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					ouvrirDialogSupprimerSalon();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					e1.printStackTrace();
				}
			}
		});
		mnSalon.add(mntmSupprimerUnSalon);
		
		JMenu mnStatut = new JMenu("Statut");
		menuBar.add(mnStatut);
		
		JRadioButtonMenuItem rdbtnmntmDisponible = new JRadioButtonMenuItem("Disponible");
		buttonGroup.add(rdbtnmntmDisponible);
		rdbtnmntmDisponible.setSelected(true);
		mnStatut.add(rdbtnmntmDisponible);
		
		JRadioButtonMenuItem rdbtnmntmOccup = new JRadioButtonMenuItem("Occupé");
		buttonGroup.add(rdbtnmntmOccup);
		mnStatut.add(rdbtnmntmOccup);
		
		JRadioButtonMenuItem rdbtnmntmAbsent = new JRadioButtonMenuItem("Absent");
		buttonGroup.add(rdbtnmntmAbsent);
		mnStatut.add(rdbtnmntmAbsent);
		
		JMenu mnAide = new JMenu("Aide");
		menuBar.add(mnAide);
		
		JMenuItem mntmManuel = new JMenuItem("Manuel");
		mnAide.add(mntmManuel);
		
		JMenuItem mntmAPropos = new JMenuItem("A propos...");
		mnAide.add(mntmAPropos);
	}
	
	public void fermerFenetre() {
		this.controleur.fermerConnexion();
		this.controleur.fermerFenetreChat();
	}
	
	public void ouvrirDialogCreerSalon()
	{
		this.controleur.ouvrirDialogCreerSalon();
	}
	
	public void ouvrirDialogRejoindreSalon() throws SQLException, ClassNotFoundException
	{
		this.controleur.ouvrirDialogRejoindreSalon();		
	}
	
	public void ouvrirDialogSupprimerSalon() throws SQLException, ClassNotFoundException
	{
		this.controleur.ouvrirDialogSupprimerSalon();
	}
	
	public void confirmerDeconnexion()
	{
		int option = JOptionPane.showConfirmDialog(this, "Êtes-vous sûr de vouloir vous déconnecter ?", "Déconnexion", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		
		if(option == JOptionPane.YES_OPTION)
		{
			//déconnexion
			this.controleur.fermerConnexion();
			this.controleur.ouvrirDialogConnexion();
			fermerFenetre();
		}
	}
	
}
